﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace MedicalPCTerminal.Write.API.Filters
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class MyExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<MyExceptionFilter> logger;

        /// <summary>
        /// 初始化全局异常过滤器
        /// </summary>
        /// <param name="logger">日志记录器</param>
        public MyExceptionFilter(ILogger<MyExceptionFilter> logger)
        {
            this.logger = logger;
        }

        /// <summary>
        /// 异常处理方法
        /// </summary>
        /// <param name="context">异常上下文</param>
        void IExceptionFilter.OnException(ExceptionContext context)
        {
            logger.LogInformation("全局异常" + context.Exception.Message);
            context.ExceptionHandled = true;
        }
    }
}
